package jlts.server;

import jlts.entity.User;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserDao
 * @Description TODO
 * @Author Zxeao
 * @Date 2021/12/23 下午5:32
 * @Version V1.0
 */
public class UserDao {
    private List<User> users=new ArrayList();

    public UserDao() {
        //当前包找一个user.txt
        try {
            InputStream is=this.getClass().getResource("user.txt").openStream();
            InputStreamReader isr=new InputStreamReader(is, "utf-8");
            BufferedReader br=new BufferedReader(isr);
            String str=null;
            while((str=br.readLine())!=null){
                //解析str 1000:海鸥:1234
                String[] tmp=str.split(":");
                User u=new User();
                u.setUname(tmp[0]);
                u.setRealName(tmp[1]);
                u.setUpwd(tmp[2]);
                users.add(u);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**根据用户名和密码找到用户并返回*/
    public User getUser(String uname,String upwd){
        for(User u:users){
            if(u.getUname().equals(uname)&& u.getUpwd().equals(upwd)){
                return u;
            }
        }

        return null;
    }
}
